// pages/user-course/user-course.js
const app = getApp()
const constant = require("../../constant.js")
const storage = require("../../utils/storage.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      qiniu: constant.resource.qiniu,
      host: constant.api(app.globalData.pord).url,
      unionid: wx.getStorageSync(storage.keys.unionid)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading({
      title: "正在加载中..."
    })

    var that = this;
    console.log(that.data.host + '/profile/courses?unionid=' + that.data.unionid)
    wx.request({
      url: that.data.host + '/profile/courses?unionid=' + that.data.unionid,
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        if (res.data.code == "0") {
          that.setData({
            appointAvailableCourseRounds: res.data.data.appointAvailableCourseRounds,
            appointUnavailableCourseRounds: res.data.data.appointUnavailableCourseRounds,
            onlineRounds: res.data.data.onlineRounds
          });

          wx.hideLoading();
        } else {
          wx.hideLoading();

          wx.showToast({
            title: res.data.message ? res.data.message : '请求失败',
            icon: 'none',
            duration: 1000
          })
        }
      },
      fail: function (res) {
        wx.hideLoading();

        wx.showToast({
          title: '请求数据失败',
          icon: 'none',
          duration: 1000
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  onAppoint: function (e) {
    var that = this;
    var finished=e.currentTarget.dataset.finished;
    var needTarget = e.currentTarget.dataset.needTarget;
    console.log("finished is "+finished);
    console.log("needTarget " + needTarget );
    var courseRoundId = e.currentTarget.dataset.id;
    if (e.currentTarget.dataset.needTarget ) {
      wx.showModal({
        title: '是否录入目标？',
        content: '您有需要填写的目标数据未填写，是否进入录入页面?',
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: 'user-course-detail/body-data/body-data?courseRoundId=' + courseRoundId + '&fromAppoint=true'
            });
          } else if (res.cancel) {
            wx.request({
              url: that.data.host + '/profile/courses/' + courseRoundId + '/target/full-in-target/cancel?unionid=' + that.data.unionid,
              method: 'POST',
              dataType: 'json',
              responseType: 'text',
              success: function (res) {
                if (res.data.code == "0") {
                  wx.navigateTo({
                    url: 'user-course-detail/course-appoint/course-appoint?courseRoundId=' + courseRoundId
                  });
                } else {
                  wx.showToast({
                    title: res.data.message ? res.data.message : '操作失败',
                    icon: 'none',
                    duration: 1000
                  })
                }
              },
              fail: function (res) {
                wx.showToast({
                  title: '请求失败',
                  icon: 'none',
                  duration: 1000
                })
              }
            })
          }
        }
      })
    } else {
      wx.navigateTo({
        url: 'user-course-detail/course-appoint/course-appoint?courseRoundId=' + courseRoundId
      });
    }
  }
})